package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"regexp"
	"strings"
)

func main() {
	// https://scratch.mit.edu/projects/editor/

	// 1、解析html文件
	fmt.Println("parse html start...")
	urls, err := parseHtml()
	if err != nil {
		fmt.Println("parse html err: ", err)
		return
	}

	if len(urls) == 0 {
		fmt.Println("none")
		return
	}
	fmt.Println("parse html end...")

	// 2、创建目录
	fmt.Println("create dir start...")
	createDir(urls)
	fmt.Println("create dir end...")

	// 3、下载
	fmt.Println("download start...")
	download(urls)
	fmt.Println("download end...")
}

// 解析html
func parseHtml() ([]string, error) {
	filename := "projects/editor/index.html"
	f, err := os.Open(filename)

	if err != nil {
		return nil, err
	}
	defer f.Close()

	b, _ := io.ReadAll(f)
	s := string(b)

	hrefRule := `href="[^http](.*)"`
	srcRule := `src="[^http](.*)"`

	urlRes := make([]string, 0)

	hrefReg := regexp.MustCompile(hrefRule)
	hrefRes := hrefReg.FindAllStringSubmatch(s, -1)
	for _, v := range hrefRes {
		urlRes = append(urlRes, v[1])
	}

	srcReg := regexp.MustCompile(srcRule)
	srcRes := srcReg.FindAllStringSubmatch(s, -1)
	for _, v := range srcRes {
		urlRes = append(urlRes, v[1])
	}

	// fmt.Println(hrefRes)
	// fmt.Println(srcRes)
	// fmt.Println(urlRes)

	return urlRes, nil
}

// 创建目录
func createDir(data []string) {
	m := make(map[string]bool)

	for _, v := range data {
		if _, ok := m[v]; !ok {
			arr := strings.Split(v, "/")
			s := strings.Join(arr[:len(arr)-1], "/")

			if len(s) == 0 {
				continue
			}

			if err := os.MkdirAll(s, 0666); err == nil {
				m[v] = true
			} else {
				fmt.Println("create dir err: ", err)
			}
		}
	}
}

// 下载
func download(data []string) {
	for _, v := range data {
		s := fmt.Sprintf("%s%s", "https://scratch.mit.edu/", v)
		// resp, err := http.Get(s)
		req, _ := http.NewRequest(http.MethodGet, s, nil)
		req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")

		// 设置代理
		tr := &http.Transport{
			Proxy: http.ProxyURL(&url.URL{
				Scheme: "http",
				Host:   "127.0.0.1:7890",
			}),
		}
		client := &http.Client{
			Transport: tr,
		}

		// 发起http的get请求
		resp, err := client.Do(req)

		if err != nil {
			fmt.Println("http request err: ", err)
			continue
		}

		defer resp.Body.Close()

		// 读取内容
		b, err := io.ReadAll(resp.Body)
		if err != nil {
			fmt.Println("read body err: ", err)
			continue
		}

		// 写入文件
		err = os.WriteFile(v, b, 0666)
		if err != nil {
			fmt.Println("write file err: ", err)
		}
	}
}
